﻿@{
    ViewBag.Title = "Agent";
    Layout = "~/Views/Shared/_Base.cshtml";
}
<table border="0" width="100%">
    <tr>
        <td valign="middle" align="left" width="350px">
            <span style="font-size: 14px">关键字:</span>
            <input id="txtKey" name="txtKey" type="text" class="input-medium search-query" placeholder="用户名/姓名/手机/Email"
                style="width: 250px; background-color: #fff;" />
        </td>
        <td valign="middle" align="left">
            <button class="btn btn-primary btn-small" id="btnSearch">
                查询
            </button>
        </td>
        <td align="right">
            <button id="btnAdd" class="btn btn-primary btn-small" style="margin-right: 20px">
                新增经纪人
            </button>
        </td>
    </tr>
</table>
 <div style="width:99%;height:1px;overflow:hidden;background-color:#dddddd;margin:10px"></div>
<table id="membergrid" class="mmg">
    <tr>
        <th rowspan="" colspan="">
        </th>
    </tr>
</table>
<div id="pg" style="text-align: right;">
</div>
<div id="agentform" class="modal fade" data-backdrop="static" tabindex="-1" style="display: none;
    width: 700px">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
            ×</button>
        <h3 class="modal-title" style="font-size: 17px; font-weight: bold" id="modaltitle">
            新增经纪人</h3>
    </div>
    <table border="0" cellpadding="0" cellspacing="0" style="margin-top:10px;">
        <tr>
            <td valign="middle" width="100px" align="right">
                用户名:
            </td>
            <td>
                <input type="text" id="txtUserName" placeholder="用户名" class="col-xs-10 col-sm-5" /><span
                    style="color: Red">*</span>
            </td>
            <td valign="middle" align="right" width="100px">
                真实姓名:
            </td>
            <td>
                <input type="text" id="txtRealName" placeholder="真实姓名" class="col-xs-10 col-sm-5" /><span
                    style="color: Red">*</span>
            </td>
        </tr>
        <tr>
            <td valign="middle" align="right">
                身份证号:
            </td>
            <td>
                <input type="text" id="txtAgentIDNO" placeholder="身份证号" class="col-xs-10 col-sm-5" /><span
                    style="color: Red">*</span>
            </td>
            <td valign="middle" align="right">
                手机号码:
            </td>
            <td>
                <input type="text" id="txtAgentHandPhone" placeholder="手机号码" class="col-xs-10 col-sm-5" /><span
                    style="color: Red">*</span>
            </td>
        </tr>
        <tr>
            <td valign="middle" align="right">
                结算方式:
            </td>
            <td valign="middle">
                <select id="settlementType" name="settlementType" class="selectpicker" data-style="btn-info">
                    <option value="0">按比例</option>
                    <option value="1">固定值</option>
                </select>
                <span style="color: Red">*</span>
            </td>
            <td valign="middle" align="right">
                结算值:
            </td>
            <td>
                <input type="text" id="txtSettlement" placeholder="结算值" class="col-xs-10 col-sm-5" /><span style="color: Red">*</span>
            </td>
        </tr>
        <tr>
            <td valign="middle" align="right">
                居住地址:
            </td>
            <td colspan="3">
                <input type="text" id="txtAddress" placeholder="居住地址" style="width:545px" class="input-xxlarge col-xs-10 col-sm-5" /><span style="color: Red">*</span>
            </td>
        </tr>
        <tr>
            <td valign="middle" align="right">
                &nbsp;
            </td>
            <td colspan="3">
                <span id="lblmsg" style="color: Red">&nbsp;</span>
                <input type="hidden" name="isEdit" id="isEdit" value="0" />
            </td>
        </tr>
    </table>
    <div class="modal-footer">
        <button id="btnSave" type="button" class="btn btn-primary">
            保存</button>
        <button type="button" data-dismiss="modal" class="btn btn-default">
            取消</button>
    </div>
</div>
@section tail{
    <script src="/content/plugins/mmgrid/mmGrid.js" type="text/javascript"></script>
    <script src="/content/plugins/mmgrid/mmPaginator.js" type="text/javascript"></script>
    @*<script src="/content/plugins/bootbox.min.js"></script>*@
    <script type="text/javascript">
        var agentform = $("#agentform");
        var cols = [
        { title: '编码', name: 'AgentCode', width: 40, align: 'center', renderer: function (val, item, index) {
            return item["AgentInfo"]["AgentCode"];
        }
        },
        { title: '用户名', name: 'UserName', width: 150, align: 'center' },
        { title: '真实姓名', name: 'RealName', width: 60, align: 'center' },
        { title: '身份证号', name: 'IDCardNo', width: 130, align: 'center' },
        { title: '手机', name: 'HandPhone', width: 80, align: 'center' },
        { title: '邮箱', name: 'Email', width: 150, align: 'center' },
        { title: '结算方式', name: 'SettlementType', width: 50, align: 'center', renderer: function (val, item, index) {
            if (item["AgentInfo"]["SettlementType"] == 0) { return '按比例'; }
            return '固定值';
        }
        },
        { title: '结算值/笔', name: 'Settlement', width: 70, align: 'center', renderer: function (val, item, index) {
            if (item["AgentInfo"]["SettlementType"] == 0) return item["AgentInfo"]["Settlement"] + "%";
            return "&yen" + Number(item["AgentInfo"]["Settlement"]).toFixed(2);
        }
        },
        { title: '状态', name: 'Status', width: 40, align: 'center', renderer: function (val, item, index) {

            if (val == 0) return '禁用';
            return '正常';
        }
        },
        { title: '操作', name: 'UserName', width: 220, align: 'center', lockWidth: true, lockDisplay: true, renderer: function (val, item, index) {
            var html = "";

            html = '<button class="btn btn-info btn-mini" onclick="grid_agent_edit(\'' + item["UserName"] + '\',\'' + item["RealName"] + '\',\'' + item["HandPhone"] + '\',\'' + item["IDCardNo"] + '\',\'' + item["Address"] + '\',\'' + item["AgentInfo"]["SettlementType"] + '\',\'' + item["AgentInfo"]["Settlement"] + '\')">编辑</button>';
            if (item['Status'] == 1) {
                html += ' <button  class="btn btn-info btn-mini" onclick="grid_agent_changestate(\'' + item["UserName"] + '\',0)">禁用</button>';
            }
            else {
                html += ' <button  class="btn btn-info btn-mini" onclick="grid_agent_changestate(\'' + item["UserName"] + '\',1)">启用</button>';
            }
            html += ' <button class="btn btn-info btn-mini" onclick="grid_memeber_view(\'' + item["UserName"] + '\')">详细</button>';
            html += ' <button  class="btn btn-danger btn-mini"  onclick="grid_agent_changestate(\'' + item["UserName"] + '\',-1)">删除</button>';

            return html;
        }
        }
    ];

        var mmg;
        var settlementType;
        $(function () {
            settlementType = $("#settlementType").selectpicker('show');
            mmg = $('#membergrid').mmGrid({
                indexCol: true,
                indexColWidth: 25,
                height: $("body", parent.document).height() - 247,
                cols: cols,
                url: '/Member/GetAgentByPage',
                method: 'get',
                remoteSort: true,
                multiSelect: false,
                checkCol: false,
                fullWidthRows: true,
                autoLoad: false,
                plugins: [
                        $('#pg').mmPaginator()
                    ]
            });
            mmg.load();
            $("#btnSearch").click(function () { 
                search();

            });
            $("#btnAdd").click(function () {
                resetform();
                var modaltitle = $("#modaltitle");
                modaltitle.text("新增经纪人");

                agentform.modal();
            });
            $("#btnSave").click(function () {
                //save();
                //bootbox.alert("<h3>保存成功</h3>");
                $("#btnSave").text("保存中...");
                $("#btnSave").attr("disabled", true);
                save();

            });
        });

        function search() {
            mmg.setParm({ keyword: $("#txtKey").val() });
            mmg.load({ page: 1 });
        }
        function save() {
             
            var mobilereg = /^1[3|5|8]\d{9}$/;
            var idcardreg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
            var emailreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z0-9]{2,3}$/;
            var settlementreg = /^\d+(\.\d+)?$/;
            if ($('#txtUserName').val() == "") {
                $('#txtUserName').focus();
                $("#lblmsg").html('用户名不能为空');
                $("#btnSave").text("保存");
                $("#btnSave").attr("disabled", false);
                return;
            }
            if (!emailreg.test($('#txtUserName').val())) {
                $('#txtUserName').focus();
                $("#lblmsg").html('用户名格式错误');
                $("#btnSave").text("保存");
                $("#btnSave").attr("disabled", false);
                return;
            }
            if ($('#txtRealName').val() == "") {
                $('#txtRealName').focus();
                $("#lblmsg").html('真实姓名不能为空');
                $("#btnSave").text("保存");
                $("#btnSave").attr("disabled", false);
                return;
            }
            if ($('#txtAgentIDNO').val() == "") {
                $('#txtAgentIDNO').focus();
                $("#lblmsg").html('身份证号码不能为空');
                $("#btnSave").text("保存");
                $("#btnSave").attr("disabled", false);
                return;
            }
            if (!idcardreg.test($('#txtAgentIDNO').val())) {
                $('#txtAgentIDNO').focus();
                $("#lblmsg").html('身份证号码格式错误');
                $("#btnSave").text("保存");
                $("#btnSave").attr("disabled", false);
                return;
            }
            if ($('#txtAgentHandPhone').val() == "") {
                $('#txtAgentHandPhone').focus();
                $("#lblmsg").html('手机不能为空');
                $("#btnSave").text("保存");
                $("#btnSave").attr("disabled", false);
                return;
            }
            if (!mobilereg.test($('#txtAgentHandPhone').val())) {
                $('#txtAgentHandPhone').focus();
                $("#lblmsg").html('手机号码输入错误');
                $("#btnSave").text("保存");
                $("#btnSave").attr("disabled", false);
                return;
            }
            if ($('#txtAddress').val() == "") {
                $('#txtAddress').focus();
                $("#lblmsg").html('居住地址不能为空');
                $("#btnSave").text("保存");
                $("#btnSave").attr("disabled", false);
                return;
            }
            if ($('#txtSettlement').val() == "") {
                $('#txtSettlement').focus();
                $("#lblmsg").html('结算值不能为空');
                $("#btnSave").text("保存");
                $("#btnSave").attr("disabled", false);
                return;
            }
            if (!settlementreg.test($('#txtSettlement').val())) {
                $('#txtSettlement').focus();
                $("#lblmsg").html('结算值输入错误');
                $("#btnSave").text("保存");
                $("#btnSave").attr("disabled", false);
                return;
            }

            $.ajax({
                url: "/Member/AgentSave",
                data: "userName=" + $('#txtUserName').val() + "&realName=" + $("#txtRealName").val()
                    + "&idCardNo=" + $('#txtAgentIDNO').val() + "&handPhone=" + $("#txtAgentHandPhone").val()
                    + "&address=" + $("#txtAddress").val()
                    + "&optype=" + $("#isEdit").val()
                    + "&settlementType=" + settlementType.val()
                    + "&settlement=" + $("#txtSettlement").val(),
                type: "post",
                success: function (result) {
                    //写提交ajax操作
                    if (result == "1") {
                        agentform.modal("hide");
                        bootbox.alert("<span style='font-size:14px;'><i style='color:green;' class=' icon-ok-sign bigger-290'></i>&nbsp;&nbsp;经纪人保存成功</span>", '300px', function () {
                          search();
                        });
                        resetform();
                    }
                    else {
                        if (result == "-1") {
                            bootbox.alert("<span style='font-size:14px;'><i style='color:green;' class='icon-warning-sign bigger-290'></i>&nbsp;&nbsp;经纪人用户名已存在</span>", '300px');

                        }
                        else if (result == "-2") {
                            bootbox.alert("<span style='font-size:14px;'><i style='color:green;' class='icon-warning-sign bigger-290'></i>&nbsp;&nbsp;身份证号码错误</span>", '300px');
                             
                        }
                        else {
                            bootbox.alert("<span style='font-size:14px;'><i style='color:green;' class='icon-remove-sign bigger-290'></i>&nbsp;&nbsp;经纪人保存失败</span>", '300px');
                        }
                    }
                    $("#btnSave").text("保存");
                    $("#btnSave").attr("disabled", false);
                }
            });
             
        }

        function grid_agent_edit(userName, realName, handphone, idcardno, address, settlementtype, settlement) {
            
            setfrom(userName, realName, handphone, idcardno, address, settlementtype, settlement);
            $("#txtUserName").attr("readonly", "readonly");
            var modaltitle = $("#modaltitle");
            modaltitle.text("编辑经纪人");
            agentform.modal();
        }

        function grid_agent_changestate(userName, state) {

            var statestr = "禁用";
            if (state == 1) {
                statestr = "启用";
            }
            else if (state == -1) {
                statestr = "删除";
            }

            bootbox.confirm("<span style='font-size:14px;'><i style='color:green;' class='icon-question-sign bigger-290'></i> &nbsp;&nbsp;确定要" + statestr + "该经纪人吗？</span>", '300px', function (confirmed) {
                if (confirmed == true) {
                    $.ajax({
                        url: "/Member/AgentChangeStatus",
                        data: "userName=" + userName + "&status=" + state,
                        type: "post",
                        cache: false,
                        success: function (result) {

                            if (result == "1") {
                                bootbox.alert("<span style='font-size:14px;'><i style='color:green;' class='icon-ok-sign bigger-290'></i>&nbsp;&nbsp;" + statestr + "经纪人成功</span>", '300px');
                                mmg.load();
                            } else {
                                bootbox.alert("<span style='font-size:14px;'><i style='color:green;' class='icon-remove-sign bigger-290'></i>&nbsp;&nbsp;" + statestr + "经纪人失败</span>", '300px');

                            }
                        }
                    });
                }
            });

        }
        function resetform() {
            settlementType.val("0");
            settlementType.selectpicker('render');
            $("#txtUserName").removeAttr("readonly");
            $("#txtUserName").val("");
            $("#txtRealName").val("");
            $("#txtAgentIDNO").val("");
            $("#txtAgentHandPhone").val("");
            $("#txtSettlement").val("");
            $("#txtAddress").val("");
            $("#isEdit").val("0");
            $("#lblmsg").html("");
        }

        function setfrom(userName, realName, handphone, idcardno, address, settlementtype, settlement) {
            settlementType.val(settlementtype);
            settlementType.selectpicker('render');
            $("#txtUserName").val(userName);
            $("#txtUserName").val(userName);
            $("#txtRealName").val(realName);
            $("#txtAgentIDNO").val(idcardno);
            $("#txtAgentHandPhone").val(handphone);
            $("#txtSettlement").val(settlement);
            $("#txtAddress").val(address);
            $("#isEdit").val("1");
            $("#lblmsg").html("");
        }
        function grid_memeber_view(userName) {
            location.href = decodeURIComponent("/Member/MemeberDetail?userName=" + userName+"&backUrl=/Member/Agent");
        }
    </script>
}
